# Shared shell functions

# Figure out the correct download server
get_download_server() {
	# Use a cache file to store the download server location
	if [ -f /tmp/smx-download-server.txt ] && test `find "/tmp/smx-download-server.txt" -mmin -60`
	then
		# Fresh cache file
    		echo -n "Fresh cache file for download server found, using content: "
		download_server=`cat /tmp/smx-download-server.txt`
	else
		# No cache file, or cache expired
		# Let's remove the file, just in case of ownership incompatibility
		rm -f /tmp/smx-download-server.txt
		echo -n "Cache file expired or non-existant, fetching download server: "
		# Fetch new value from the server
		download_server=`curl -s http://www.sumilux.com/get-download-server.php`
		# Save result to the cache file
		echo -n $download_server > /tmp/smx-download-server.txt
	fi
	echo $download_server

	# Temporary implementation, effectively a global variable
	# download_server=release-hf.sumilux.com
}

# Download a file from server
#  $1: File Name (fixed path on the server)
#  $2: Local Path
download_file() {
	# First figure out where the download server is
	get_download_server

 	echo Downloading file name $1, from server $download_server, to local path $2
	REMOTE_USER=stevel
	REMOTE_SERVER=$download_server
	rsync -avzc ${REMOTE_USER}@${REMOTE_SERVER}::release/war/$1 --force $2
}
